package com.flying.controller;

import com.flying.entity.UserContext;
import com.flying.feign.ProviderClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/test")
public class ConsumerController {

    //自动转载，所以直接用
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProviderClient providerClient;

    @GetMapping("/instances")
    public List<ServiceInstance> instances(){

        List<ServiceInstance> provider = this.discoveryClient.getInstances("provider");
        return provider;
    }

    @GetMapping("/test1")
    public String test1(){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String user = request.getHeader("x-user");
        System.out.println(user);
        String token = request.getHeader("token");
        System.out.println("=="+token);
        return providerClient.list();
    }
    @GetMapping("/test2")
    public String test2(){
        System.out.println("test2");
        return "test2";
    }
}
